package com.oaec.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestServlet
 */
//@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        System.out.println("我的构造方法被调用了");
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		String creater=(String)config.getInitParameter("creater");

		System.out.println("初始化servlet,得到配置参数creater为："+creater);
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		System.out.println("销毁servlet");
	}

	/**
	 * @see Servlet#getServletConfig()
	 */
	public ServletConfig getServletConfig() {
		System.out.println("获得servlet的配置项");
		return null;
	}

	/**
	 * @see Servlet#getServletInfo()
	 */
	public String getServletInfo() {
		System.out.println("获得servlet的信息");
		return null; 
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("我正在提供服务");
		
		response.setCharacterEncoding("UTF-8");//告诉服务端输出流，说你要以utf-8输出以下内容
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		out.write("\r\n");
	      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
	      out.write("<html>\r\n");
	      out.write("<head>\r\n");
	      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
	      out.write("<title>Insert title here</title>\r\n");
	      out.write("</head>\r\n");
	      out.write("<body>\r\n");
	      out.write("\t<form action=\"login\" method=\"post\">\r\n");
	      out.write("\t\t<table border=\"1\">\r\n");
	      out.write("\t\t\t<tr><td>用户名：</td><td><input type=\"text\" name=\"user_name\"></td></tr>\r\n");
	      out.write("\t\t\t<tr><td>密码：</td><td><input type=\"password\" name=\"user_pass\"></td></tr>\r\n");
	      out.write("\t\t\t<tr><td colspan=\"2\"><input type=\"submit\" value=\"登录\"></td></tr>\r\n");
	      out.write("\t\t</table>\r\n");
	      out.write("\t</form>\r\n");
	      out.write("</body>\r\n");
	      out.write("</html>");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("我正在提供服务，来自于get方式的请求");
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("我正在提供服务，来自于pot方式的请求");
		doGet(request, response);
	}

}
